package com.itheima.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 类 名: OrderSettingController
 * 描 述:
 * 作 者: 蔡畅
 * 创 建：2019年11月23日
 */
@RestController
@RequestMapping("/orderSetting")
public class OrderSettingController {
    @Reference
    private OrderSettingService orderSettingService;

    /**
     * 根据指定日期修改可预约人数
     * @param orderSetting
     * @return
     */
    @RequestMapping("/editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting){
        //预约成功设置
        try {
            orderSettingService.editNumberByDate(orderSetting);
            return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            //预约失败设置
            return new Result(false, MessageConstant.ORDERSETTING_FAIL);
        }
    }


    @RequestMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String date) {
        List<Map> list = orderSettingService.getOrderSettingByMonth(date);
        //获取预约设置数据成功
        try {
            return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS,list);
        } catch (Exception e) {
            e.printStackTrace();
            //获取预约设置失败
            return new Result(false, MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }


//    /**
//     * Excel文件上传，并解析文件内容保存到数据库
//     * @param excelFile
//     * @return
//     */
//    @RequestMapping("/upload")
//    public Result upload(@RequestParam("excelFile")MultipartFile excelFile){
//        try {
//            //读取Excel文件数据
//            List<String[]> list = POIUtils.readExcel(excelFile);
//            if(list != null && list.size() > 0){
//                List<OrderSetting> orderSettingList = new ArrayList<>();
//                for (String[] strings : list) {
//                    OrderSetting orderSetting = new OrderSetting(new Date(strings[0]), Integer.parseInt(strings[1]));
//                    orderSettingList.add(orderSetting);
//                }
//                orderSettingService.setNumberByDateBatch(orderSettingList);
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//            return new Result(false, MessageConstant.IMPORT_ORDERSETTING_FAIL);
//        }
//        return new Result(true,MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
//    }


    private boolean isNotEmpty(Object o){
        if(null == o){
            return false;
        }
        if(o instanceof  String){
            String o1 = (String) o;
            if(o1.equals("") || o1.equals(" ")){
                return false;
            }
        }
        return true;
    }

    @RequestMapping("/upload")
    public Result upload(MultipartFile excelFile) throws IOException {
//        使用poiutil读取Excel 返回一个list list里面装的是一个一个数组
        List<String[]> dataList = POIUtils.readExcel(excelFile);
        //把dataList转成List<OrderSetting>
        List<OrderSetting> orderSettings = new ArrayList<>();
        if(CollectionUtil.isNotEmpty(dataList)){
            for (String[] array : dataList) {
                if(array.length != 2){//数据一定要完整
                    continue;
                }
                String dateStr = array[0];//yyyy/MM/dd
                String numberStr = array[1];
                if(isNotEmpty(dateStr) && isNotEmpty(numberStr)){
                    OrderSetting orderSetting = new OrderSetting();
                    orderSetting.setOrderDate(DateUtil.parse(dateStr,"yyyy/MM/dd"));
                    orderSetting.setNumber(Integer.valueOf(numberStr));
                    orderSettings.add(orderSetting);
                }

            }
        }
        orderSettingService.setNumberByDateBatch(orderSettings);
        return new Result(true,MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
    }
}



